package light.head.dao.oa;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import light.head.enm.RESULT;
import light.head.framework.base.BaseDAO;
import light.head.framework.entity.PageBean;
import light.head.model.ProcessConfig;

import org.springframework.stereotype.Service;
/***
 * 流程配置
 * @author yy
 *
 */
@Service
public class ProcessConfigDAO extends BaseDAO<ProcessConfig, Integer>{
	private final static String sql_findByRc = "FROM ProcessConfig pc WHERE pc.rc = ? AND pc.deleted = 0 AND pc.business = ?";
	private final static String sql_findByOcBusiness = "FROM ProcessConfig pc WHERE pc.oc = ? AND pc.deleted = 0 AND pc.business = ?";
	private final static String sql_getByBusinessRc = "FROM ProcessConfig p WHERE p.business = ? AND p.rc = ? AND p.oc = ?";
	private final static String sql_checkByBusinessRc = "SELECT a.status FROM AuditStatus a JOIN ProcessConfig p ON p.code = a.pcc WHERE p.business = ? AND p.rc = ? AND p.oc = ?";
	
	private final static String sql_findByOc = "FROM ProcessConfig prc WHERE prc.oc = ? AND prc.deleted = 0 ORDER BY prc.business , prc.pc ASC";
	
	public RESULT findByMap(Integer rootCode, Map<String, String >map, PageBean pb){
		if (null == rootCode) return null;
		StringBuffer hql = new StringBuffer("FROM ProcessConfig pc WHERE pc.oc = ?");
		List<Object> list = new ArrayList<Object>();
		list.add(rootCode);
		
		hql.append(" ORDER BY pc.code DESC");
		
		return findToPageBean(hql.toString(), pb, list);
	}
	
	/**
	 * 通过机构获取所有的配置节点
	 * @param oc 机构编号
	 */
	public List<ProcessConfig> findByOc(Integer oc) {
		if(oc == null) return null;
			return find(sql_findByOc, oc);
	}
	
	/**
	 * 通过机构编号和业务类型获取所有的配置节点
	 * @param oc	机构编号
	 * @param business	业务类型
	 */
	public List<ProcessConfig> findByOcBusiness(Integer oc, Byte business){
		if(oc == null || null == business) return null;
			return find(sql_findByOcBusiness, oc, business);
	}
	
	/**
	 * 
	 * @param rc
	 * @return
	 */
	public List<ProcessConfig> findByRc(Integer rc,Byte business){
		if(rc == null) return null;
			return find(sql_findByRc, rc,business);
	}
	
	/**
	 * 通过业务类型和角色编号获取审核配置节点
	 * @param business 业务类型
	 * @param rc	角色编号
	 */
	public ProcessConfig getByBusinessRc(Byte business, Integer rc, Integer oc) {
		if (null == business || null == rc || null == oc) return null;
		return (ProcessConfig) get(sql_getByBusinessRc, business, rc, oc);
	}
	
	/**
	 * 通过业务类型和角色编号获取当前的节点的审核状态
	 * @param business 业务类型
	 * @param rc	角色编号
	 * @param oc	机构编号
	 */
	public Byte checkByBusinessRc(Byte business, Integer rc, Integer oc) {
		if (null == business || null == rc || null == oc) return null;
		Object obj = query(sql_checkByBusinessRc, null, 1, business, rc, oc);	// 获取审核状态
		if (null == obj) return null;
		Object[] auditStatus = (Object[]) obj;	// 转成  object数组
		return Byte.parseByte(auditStatus[0].toString());
	}
	/**
	 * 
	 * @param rc
	 * @param oc
	 * @return
	 */
	public Integer ishave(Integer rc, Integer oc){
		if ( null == rc || null == oc) return null;
		StringBuffer sql = new StringBuffer("SELECT pc.pc FROM ProcessConfig pc WHERE pc.rc = ? AND pc.oc = ?");
		
		List<Object> parm = new ArrayList<Object>();
		parm.add(rc);
		parm.add(oc);
		
		List<Object> objList = query(sql.toString(), null, 1, parm.toArray());	// 获取审核状态
		return (null == objList || objList.size() == 0) ? null : Integer.parseInt(objList.get(0).toString());
	}
	
	/**
	 * 通过当前节点的业务类型和角色编号获取上一个节点的审核状态
	 * @param business	业务类型
	 * @param rc	角色编号
	 * @param oc	机构编号
	 */
	public Byte checkBeforeByBusinessRc(Byte business, Integer rc, Integer oc) {
		StringBuffer sql = new StringBuffer("SELECT a.status FROM AuditStatus a");
		sql.append(" WHERE a.pcc = (");
		sql.append(" SELECT p.code FROM ProcessConfig p WHERE p.business = ? AND p.oc = ?");
		sql.append(" AND p.pc = (SELECT p1.pc - 1 FROM ProcessConfig p1 WHERE p1.business = ? AND p1.oc = ? AND p1.rc = ?)");
		sql.append(")");
		
		List<Object> parm = new ArrayList<Object>();
		parm.add(business);
		parm.add(oc);
		
		parm.add(business);
		parm.add(oc);
		parm.add(rc);
		
		List<Object> objList = query(sql.toString(), null, 1, parm.toArray());	// 获取审核状态
		if (null == objList || objList.size() == 0) return null;
		Object[] auditStatus = (Object[]) objList.get(0);	// 转成  object数组
		
		return Byte.parseByte(auditStatus[0].toString());
	}
	
	
	public RESULT update(ProcessConfig pc) {
		if(pc == null ||  null == pc.getCode()){
			return RESULT.PARAMETER_ERROR;
		}
		ProcessConfig old = get(pc.getCode());
		
		if (null == old)
			return RESULT.NOT_FIND;
		
		Boolean ok = false;
		if (null != old.getRc() || (null != pc.getRc() && old.getRc().intValue() != pc.getRc().intValue())) {
			ok = true;
			old.setRc(pc.getRc());
		}
		if (null != pc.getPc()  || (null != pc.getPc() && old.getPc().intValue() != pc.getPc().intValue())) {
			ok = true;
			old.setPc(pc.getPc());
		}
		
		if (ok)
			return upd(old);
		
		return RESULT.SUCCESS;
	}

	
}
